<?php

use App\Models\Admin\Store;

function v($msg)
{
    echo '<pre>';
    var_dump($msg);
    exit;
}

/**
 * 成功返回
 * @param $data
 * @param string $msg
 */
function successMsg($data = [], $msg = "成功")
{
    $info = [
        'code' => 1,
        'msg' => $msg,
        'data' => $data
    ];
    echo json_encode($info, JSON_UNESCAPED_UNICODE);
    exit;
}

/**
 * 失败返回
 * @param string $msg
 * @param string $code
 * @param array $data
 */
function errorMsg($msg = "失败", $code = "1002", $data = [])
{
    $info = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data
    ];
    echo json_encode($info, JSON_UNESCAPED_UNICODE);
    exit;
}

/**
 * ******************
 * 1、写入内容到文件,追加内容到文件
 * 2、打开并读取文件内容
 * *******************
 */
function saveLog($msg, $case = 'default')
{
    $path = base_path() . "/storage/mylogs/{$case}/";
    if (!is_dir($path)) {
        mkdir($path, 0777, true);
    }
    if (is_array($msg)) {
        $msg = json_encode($msg, JSON_UNESCAPED_UNICODE);
    }
    $filename = $path . date('Ymd') . '.txt';
    if (file_exists($filename)) {
        //        chmod($filename, 0777);
    }
    $content = date("Y-m-d H:i:s") . " ==> " . $msg . "\r\n\r\n";
    file_put_contents($filename, $content, FILE_APPEND);
}

//加密函数
function lockToken($txt = '', $key = 'enc')
{
    if ($txt) {
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
        //        $nh    = rand(0, 64);
        $nh = 8;
        $ch = $chars[$nh];
        $mdKey = md5($key . $ch);
        $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
        $txt = base64_encode($txt);
        $tmp = '';
        $i = 0;
        $j = 0;
        $k = 0;
        for ($i = 0; $i < strlen($txt); $i++) {
            $k = $k == strlen($mdKey) ? 0 : $k;
            $j = ($nh + strpos($chars, $txt[$i]) + ord($mdKey[$k++])) % 64;
            $tmp .= $chars[$j];
        }
        return urlencode($ch . $tmp);
    } else {
        return false;
    }
}

//解密函数
function unlockToken($txt = '', $key = 'enc')
{
    if ($txt) {
        $txt = urldecode($txt);
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-=+";
        $ch = $txt[0];
        $nh = strpos($chars, $ch);
        $mdKey = md5($key . $ch);
        $mdKey = substr($mdKey, $nh % 8, $nh % 8 + 7);
        $txt = substr($txt, 1);
        $tmp = '';
        $i = 0;
        $j = 0;
        $k = 0;
        for ($i = 0; $i < strlen($txt); $i++) {
            $k = $k == strlen($mdKey) ? 0 : $k;
            $j = strpos($chars, $txt[$i]) - $nh - ord($mdKey[$k++]);
            while ($j < 0) {
                $j += 64;
            }
            $tmp .= $chars[$j];
        }
        return base64_decode($tmp);
    } else {
        return false;
    }
}

/**
 * 随机生成安全符号 登录校验
 * @param int $length
 * @return string
 */
function pbGetRandStr($length = 4)
{
    // 密码字符集，可任意添加你需要的字符
    $chars = array(
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
        'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
        't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
        'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
    );

    // 在 $chars 中随机取 $length 个数组元素键名
    $keys = array_rand($chars, $length);
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        // 将 $length 个数组元素连接成字符串
        $password .= $chars[$keys[$i]];
    }
    return $password;
}

/**
 * <<<<<<< HEAD
 * 获取商户编码
 * @return string
 */
function pdStoreSn($pix = 'DF')
{
    $nowTime = time();
    $startTime = strtotime(date('Y-m-01', $nowTime));
    $num = Store::where('created_at', '>=', $startTime)->count();
    $str1 = date('Ym', $nowTime);
    $str2 = substr("00000" . ($num + 1), -4);
    return "{$pix}{$str1}{$str2}";
}

/**
 * 订单号生成
 * @param int $store_id
 * @param string $pix
 * @return string
 */
function pdOrderSn($num = 0, $pix = 'OD')
{
    $nowTime = time();
    $str1 = date('Ym', $nowTime);
    $str2 = substr("00000" . $num, -4);
    return "{$pix}{$str1}{$str2}";
}

/*
 * 获取树形结构
 * @param $list
 * @param int $pid
 * @return array
 */
function getTree($list, $pid = 0)
{
    $dataArr = [];
    foreach ($list as $key => $item) {
        if ($item['pid'] == $pid) {
            $item['son'] = getTree($list, $item['id']);
            $dataArr[] = $item;
        }
    }
    return $dataArr;
}

/**
 * 验证字段
 * @param $dataArr
 */
function prValidateField($dataArr)
{
    $arr1s = ['id', 'page', 'goods_id', 'type', 'order_id'];
    if ($dataArr) {
        foreach ($dataArr as $kk => $vv) {
            if (in_array($kk, $arr1s)) {
                if (!is_numeric($vv)) {
                    errorMsg('参数类型错误');
                }
            }
        }
    }
    return true;
}

/**
 * 随机生成字符串
 * @param int $length
 * @return string
 */
function randString($length = 4)
{
    # 字符集
    $chars = [
        'S', '6', 'U', 'V', 'W', '5', 'X', 'Y', 'Z', '0', '1', '2',
        'l', 'm', 'n', 'o', 'p', '7', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
        '3', 'a', 'b', 'c', 'd', '4', 'e', 'N', 'O', 'P', 'f', 'T',
        'g', 'h', 'i', 'j', 'k', 'H', 'I', 'J', '8', 'K', 'L', 'M',
        'Q', 'R',
    ];

    $keys = array_rand($chars, $length);
    $string = '';
    for ($i = 0; $i < $length; $i++) {
        $string .= $chars[$keys[$i]];
    }
    return $string;
}